# -*- coding: utf-8 -*-

'''
Created on 05.06.2011

@author: mutius
'''
import wx
from wx._core import EVT_BUTTON

class InsertIntoTableDialog(wx.Dialog):
    def __init__(self, parent, id, cursor, currentTable):
        wx.Dialog.__init__(self, parent, id)
        self.cursor = cursor
        self.currentTable = currentTable
        self.command = str()
        self.columns = self.cursor.getColumnLabels(self.currentTable)
        self.label_2 = wx.StaticText(self, -1, u"вставка значения в таблицу "+currentTable)
        self.static_line_1 = wx.StaticLine(self, -1)
        self.button_1 = wx.Button(self, -1, "Ok")
        self.labels, self.entrys = list(), list()

        self.command = str()
        self.SetTitle(u"добавление значения")
        self.SetSize((350, 180))

        for column in self.columns:
            self.labels.append(wx.StaticText(self, -1, column))
            self.entrys.append(wx.TextCtrl(self, -1, ""))
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_4 = wx.BoxSizer(wx.HORIZONTAL)    
        
        #sizer_1.Add(self.checkbox_1, 0, wx.LEFT|wx.TOP, 10)
        sizer_1.Add(self.label_2, 0, wx.LEFT|wx.TOP, 10)
        sizer_1.Add(self.static_line_1, 0, wx.ALL|wx.EXPAND, 10)
        
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
        sizer_2.Add(sizer_4, 1, wx.EXPAND, 0)
        
        for label in self.labels:
            sizer_3.Add(label, 1, wx.LEFT|wx.RIGHT|wx.ALIGN_BOTTOM, 5)
        for entry in self.entrys:
            sizer_4.Add(entry, 1, wx.LEFT|wx.RIGHT|wx.ALIGN_BOTTOM, 5)
        
        sizer_1.Add(self.static_line_1, 0, wx.ALL|wx.EXPAND, 10)
        sizer_1.Add(self.button_1, 0, wx.RIGHT|wx.BOTTOM|wx.ALIGN_RIGHT, 10)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        
        self.Bind(EVT_BUTTON, self.onOk, self.button_1)
    def onOk(self, event):
        names = list()
        for entry in self.entrys:
            names.append(entry.GetValue())
        print "ok pushed"
        #if min([len(name) for name in names]) == 0:
        #    message = wx.MessageDialog(None, u"одно или более полей заполнены не корректно", u"Message", wx.ICON_HAND)
        #    message.ShowModal()
        #    message.Destroy()
        #else:
        self.command = u"INSERT INTO " + self.currentTable + " VALUE("
        for name in names:
            self.command = self.command + name + ","
        self.command = self.command[0:-1] + ")"
        self.Close()